import api from '../common/api.js'
import store from '../store/index'
// let isPay = false
const shbPay = function (order_no, amount, cbUrl, payType = 2) {
    let isPay = 0
    return new Promise(function (reslove, reject) {
        if (isPay) { reject(false); return }   
        switch (payType) {
            case 1:
                let userAmount = store.state.userInfo.amount
                if (userAmount < amount) {
                    uni.showModal({
                        title: '提示',
                        content: `余币不足，您本次需充值${(amount - userAmount)/ 100}`,
                        success: function (res) {
                            if (res.confirm) {
                                uni.navigateTo({
                                    url: '/about/recharge?cbUrl=' + cbUrl
                                });
                            } else if (res.cancel) {
                                if (cbUrl) {
                                    uni.redirectTo({
                                        url: cbUrl
                                    });
                                }
                            }
                        }
                    });
                } else {
                    uni.showModal({
                        title: '支付确认',
                        content: `确定要支付美粉币：${amount / 100}`,
                        success: function (res) {
                            if (res.confirm) {
                                postOrderPayment()
                            } else if (res.cancel) {
                                if (cbUrl) {
                                    uni.redirectTo({
                                        url: cbUrl
                                    });
                                }
                            }
                        }
                    });
                }
                break;
            default:
                postOrderPaymentWechat()
                break;
        }



        function postOrderPayment() {
            api.postOrderPayment({ order_no }).then(res => {
                if (res.code == 200) {
                    paySuccess()
                }
            })
        }
        function postOrderPaymentWechat() {
            api.postOrderPaymentWechat({ order_no }).then(res => {
                if (res.code == 200) {
                    let { timeStamp,
                        nonceStr,
                        signType,
                        package: packages,
                        paySign } = res.data.payInfo
                    wx.requestPayment({
                        timeStamp,
                        nonceStr,
                        package: packages,
                        signType,
                        paySign,
                        success: res => {
                            paySuccess()
                        },
                        fail: res => {
                            console.error(res);
                            if (cbUrl) {
                                uni.redirectTo({
                                    url: cbUrl
                                });
                            }
                        },

                    });

                }
            })
        }
        function paySuccess() {
            isPay = true
            uni.showToast({
                title: "支付成功",
                duration: 2000
            });
            store.dispatch('refreshBalance')
            reslove(true)
            setTimeout(() => {
                if (cbUrl) {
                    uni.redirectTo({
                        url: cbUrl
                    });
                }
            }, 1000);

        }
    })
}
export default shbPay